package 算法回顾;

/**
 * @description:
 * @author: 小白白
 * @create: 2021-09-24
 **/

public class No198打家劫舍 {

    public int rob(int[] nums) {

        if (nums.length == 2) {
            return Math.max(nums[0], nums[1]);
        }

        int[] dp = new int[nums.length];

        dp[0] = nums[0];
        // 切记[1]的base是max()
        if (nums.length >= 2) {
            dp[1] = Math.max(nums[0], nums[1]);
        }

        for (int i = 2; i < nums.length; i++) {
            dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]);
        }

        return dp[nums.length - 1];
    }

    public static void main(String[] args) {
        No198打家劫舍 n = new No198打家劫舍();
        int[] arr = {2,1,1,2};
        int result = n.rob(arr);
        System.out.println(result);
    }

}
